from appium import webdriver
import time
from selenium.webdriver.common.by import By
from appium.options.android import UiAutomator2Options

import random

options = UiAutomator2Options()
options.load_capabilities(
{
  'platformName': 'Android', # 被测手机是安卓
  'platformVersion': '5.1.1', # 手机安卓版本
  'deviceName': 'ys',  # 设备名称
  'appPackage': 'com.sina.weibo', # 启动APP Package名称
  'appActivity': 'com.sina.weibo.SplashActivity', # 启动Activity名称
  'unicodeKeyboard': True, # 使用自带输入法，输入中文时填True
  'resetKeyboard': True, # 执行完程序恢复原来输入法
  'noReset': True,       # 不要重置App
  'newCommandTimeout':600,
  'automationName': 'UiAutomator2'
})

time.sleep(10)
app=webdriver.Remote("http://127.0.0.1:4723/wd/hub",options=options)
time.sleep(10)


# 登录
app.find_element(by=By.ID,value='com.sina.weibo:id/titleBack').click()
time.sleep(5)
# 输入手机号
app.find_element(by=By.ID,value='com.sina.weibo:id/et_login_view_phone').send_keys('17657371732')
time.sleep(10)
# 验证码
app.find_element(by=By.ID,value='com.sina.weibo:id/tv_login_view_resent').click()
time.sleep(20)
# 勾选
app.find_element(by=By.ID,value='com.sina.weibo:id/iv_login_view_protocol').click()
time.sleep(3)
# 登录
app.find_element(by=By.ID,value='com.sina.weibo:id/btn_login_view_sms').click()
time.sleep(5)

# 跳过
app.find_element(by=By.ID,value='com.sina.weibo:id/tv_title_bar_skip').click()
time.sleep(1)

# 跳过
app.find_element(by=By.ID,value='com.sina.weibo:id/tv_title_bar_skip').click()
time.sleep(1)

# 热门
app.find_element(By.XPATH,'//*[@text="热门"]').click()
time.sleep(5)

# 搜索
app.find_element(by=By.ID,value='com.sina.weibo:id/lySearchInput').click()
time.sleep(3)
# 搜索名称
app.find_element(by=By.ID,value='com.sina.weibo:id/tv_search_keyword').send_keys("高考")
time.sleep(5)
# 点击搜索
app.find_element(by=By.ID,value='com.sina.weibo:id/rl_coupon_des').click()
time.sleep(5)


# 实时
app.find_element(By.XPATH,'//*[@text="实时"]').click()
time.sleep(3)


# 返回
app.find_element(by=By.ID,value='com.sina.weibo:id/back').click()
time.sleep(5)
# 取消
app.find_element(by=By.ID,value='com.sina.weibo:id/btn_search_or_back').click()
time.sleep(10)

# 获取屏幕尺寸
screen = app.get_window_size()
#屏幕宽度
width = screen["width"]
#屏幕高度
height = screen["height"]
cs=0
max_cs=5
while cs<max_cs:
  try:
    watch_time=random.uniform(3,5)
    time.sleep(watch_time)
    start_x = width *0.5
    # 滑动起点
    start_y = int(height * 0.8)
    # 滑动终点
    end_y = int(height * 0.2)
    # 滑动速度
    app.swipe(start_x, start_y, start_x, end_y, duration=500)
    cs+=1
    time.sleep(2)
  except Exception as e:
    break

